package com.tvb.comm.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;

public class PropertiesUtil {
	static Log log = LogFactory.getLog(PropertiesUtil.class.getName());
	private Properties properties = new Properties();
	public List<String> names = new ArrayList<String>();

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}
	/**
	 * 获取 pro里面的值
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String get(String key,Object... defaultValue){
		if(properties == null)initProperties();
		String reult = "";
		String value = "";
		if(defaultValue != null && defaultValue.length >0){
			value = defaultValue[0].toString();
		}
		reult = properties.getProperty(key, value);
		return reult;
	}
	
	/**
	 * 初始化Properties
	 */
	private void initProperties() {
		if (names.size() == 0)
			return;
		if (properties == null)
			properties = new Properties();
		for (int i = 0; i < names.size(); i++) {
			InputStream in = null;
			try {
				in = new ClassPathResource(names.get(i)).getInputStream();
				properties.load(in);
			} catch (Exception e) {
				log.info(names.get(i)+"加载失败:"+e.getMessage());
			}finally{
				try {if(in != null)in.close();} catch (IOException e) {}
			}
		}
	}
}